# -*- coding: utf-8 -*-
# @Time : 2020/11/01 09:30
# @Author : Houor
# @Site : https://my.oschina.net/houor
# @File : Newton.py
# @Description: 牛顿法

# 需要使用二阶导数性质
def func(x):
    return x ** 4 - 4 * (x ** 3) - 6 * (x ** 2) - 16 * x + 4


def first_derivative(x):
    return 4 * (x ** 3) - 12 * (x ** 2) - 12 * x - 16


def second_derivative(x):
    return 12 * (x ** 2) - 24 * x - 12


epsilon = 0.01
flag = False
idx = 1

x1 = 6
x2 = x1

while not flag:
    f1 = first_derivative(x1)
    if abs(f1) < epsilon:
        flag = True
        print('idx = ', idx, ':')
        print('\t\tx1 = ', x1, ', end.')
        print('\t\tf\' = ', f1, ', end.')
        break

    f2 = second_derivative(x1)

    x2 = x1 - (f1 / f2)

    print('idx = ', idx, ':')
    print('\t\tx1 = ', x1, ', x2 = ', x2)
    print('\t\tf\' = ', f1, ', f\" = ', f2)
    idx = idx + 1

    x1 = x2

print('x = ', x2)
